/**
 * 依赖全局变量 InfoGlobal
 * Created by shopnc on 2015/11/26.
 */
//定义表格
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'code',
        title: '代理商Id',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk',
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'name',
        title: '代理商名称',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk',
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            return '<a href="' + ncGlobal.adminRoot + 'agent/detail?agentId=' + record.id + '">' + value + '</a>';
        }
    },
    {
        id: 'gcFee',
        title: '美黄金手续费',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false
    },
    {
        id: 'clFee',
        title: '美原油手续费',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false
    },
    {
        id: 'siFee',
        title: '美白银手续费',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false
    },
    {
        id: 'hsiFee',
        title: '恒指手续费',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false
    },
    {
        id: 'mhiFee',
        title: '小恒指手续费',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false
    },
    {
        id: 'daxFee',
        title: '德指手续费',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
    fastSort: false
    },
    {
        id: 'a50Fee',
        title: 'A50手续费',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false
    },
    {
        id: 'status',
        title: '状态',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq',
        codeTable: {
            1: $lang.agentStatus.T1,
            2: $lang.agentStatus.T2,
        }
    },
    {
        id: 'fullName',
        type: 'string',
        title: '代理商协议名称',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        hideType: 'md|sm|xs|lg'
    },
    {
        id: 'domain',
        type: 'string',
        title: '代理商域名',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false
    },
    {
        id: 'smsSign',
        title: '代理商短信签名',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false
    },
    {
        id: 'operation',
        title: '管理操作',
        type: 'string',
        columnClass: 'text-center width-200',
        fastSort: false,
        extra: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var html = "";
            if ($("#agentEdit") && $("#agentEdit").val() == 1) {
                html += "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
            }
            if ($("#agentDel") && $("#agentDel").val() == 1) {
                html += "<a href='javascript:;' class='btn btn-danger btn-sm m-r-10' onclick='information.delAgent(" + record.id + ",\"" + record.name + "\")'><i class='fa fa-trash-o'></i>&nbsp;删除&nbsp;</a>";
            }
            return html;
        }
    }

];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'agent/json/list',
    exportFileName: '代理商列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    tools: 'refresh|faseQuery',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {BigDecimal:["amount","actualArrival"],int:["status"],Timestamp: ["createTime"]}
};



var grid = $.fn.DtGrid.init(dtGridOption);
//排序
grid.sortParameter.columnId = ['desc_createTime'];
grid.fastQueryParameters = new Object();
grid.fastQueryParameters['eq_delFlag'] = 1;


/**
 * 资讯列表所使用的js
 */
var information = function () {

    //删除Url
    var delUrl = ncGlobal.adminRoot + "agent/json/del";

    function _delAgent(id, content) {
        var tpl = '您选择对代理商<strong>' + content + '</strong> 进行删除操作，删除后将无法恢复。<br/>您确定要进行删除操作吗?'
        $.ncConfirm({
            url: delUrl,
            data: {
                id: id
            },
            content: tpl,
            alertTitle: "删除操作"
        });
    }

    var addInfo = {
        initAddModal: function () {
            $addForm = $("#addForm");
            $(".alert-danger").remove();
            $addForm.psly().reset();
            $addForm.find('input[name="code"]').val("");
            $addForm.find('input[name="name"]').val("");
            $addForm.find('input[name="fullName"]').val("");
            $addForm.find('input[name="domain"]').val("");
            $addForm.find('input[name="smsSign"]').val("");
            $(".addStatus").bootstrapSwitch('state', true);
            $addForm.find('input[name="gcFee"]').val("");
            $addForm.find('input[name="clFee"]').val("");
            $addForm.find('input[name="siFee"]').val("");
            $addForm.find('input[name="hsiFee"]').val("");
            $addForm.find('input[name="mhiFee"]').val("");
            $addForm.find('input[name="daxFee"]').val("");
        }
    }

    /**
     * 事件绑定
     * @private
     */
    function _bindEvent() {
        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['eq_delFlag'] = 1;
            grid.fastQueryParameters['lk_name_or_like_code'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });

        //去除输入框回车键提交
        $("input").on("keydown", function (e) {
            if (e.keyCode == 13) {
                e.preventDefault();
                var a = $("#releasePrice");
                a.length && a.trigger("click");
            }
        });

        //新增代理商
        $('#addModal').on('show.bs.modal', function (event) {
            addInfo.initAddModal();
        });

        //编辑代理商
        $('#editModal').on('show.bs.modal', function (event) {
            //获取接受事件的元素
            var  button = $(event.relatedTarget),
                //获取data 参数
                datano = button.data('no'),
                modal = $(this),
                //获取列表框中的原始数据
                gridData = grid.sortOriginalDatas[datano],
                editForm = $("#editForm");
            //清除错误提示
            editForm.psly().reset();
            $(".alert-danger").remove();
            modal.find('input[name="id"]').val(gridData.id);
            modal.find('input[name="name"]').val(gridData.name);
            modal.find('input[name="code"]').val(gridData.code);
            modal.find('input[name="fullName"]').val(gridData.fullName);
            modal.find('input[name="domain"]').val(gridData.domain);
            modal.find('input[name="smsSign"]').val(gridData.smsSign);
            if(gridData.status == 1){
                $(".editStatus").bootstrapSwitch('state', true);
            }else{
                $(".editStatus").bootstrapSwitch('state', false);
            }
            modal.find('input[name="gcFee"]').val(gridData.gcFee);
            modal.find('input[name="clFee"]').val(gridData.clFee);
            modal.find('input[name="siFee"]').val(gridData.siFee);
            modal.find('input[name="hsiFee"]').val(gridData.hsiFee);
            modal.find('input[name="mhiFee"]').val(gridData.mhiFee);
            modal.find('input[name="daxFee"]').val(gridData.daxFee);
        });


}

    //外部可调用
    return {
        init: function () {
            _bindEvent();
        },
        delAgent : _delAgent
    }
}()
$(function () {
    grid.load();
    information.init();
})





